package com.itheima.controller.home;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.Sms.SmsHomeNewProductService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsHomeBrand;
import com.itheima.vo.pojo.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home/newProduct")
public class SmsHomeNewProductController {


    @DubboReference
    SmsHomeNewProductService homeNewProductService;


    /*
    * 1.添加首页新品
    * */
    @PostMapping("/create")
    public AjaxResult addIndexNew(@RequestBody List<SmsHomeNewProduct> smsHomeNewProduct)
    {

        boolean save = homeNewProductService.saveBatch(smsHomeNewProduct);

        return AjaxResult.success(save);
    }


    /*
    * 2.批量删除首页新品
    * */
    @PostMapping("/delete")
    public AjaxResult BatchIndexNew(@RequestParam List<Integer> ids)
    {
        boolean b = homeNewProductService.removeByIds(ids);
        return AjaxResult.success(ids);

    }

    /*
    * 3.分页查询
    * */
    @GetMapping("/list")
    public AjaxResult AllPage(Integer pageNum , Integer pageSize , String productName , Integer recommendStatus)
    {
        LambdaQueryWrapper<SmsHomeNewProduct> lambdaQueryWrapper  =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ge(productName !=null,SmsHomeNewProduct::getProductName, productName);
        lambdaQueryWrapper.like(recommendStatus !=null ,SmsHomeNewProduct::getRecommendStatus, recommendStatus);
        TableDataInfo tableDataInfo  = new TableDataInfo();
        Page<SmsHomeNewProduct> page = new Page<>(pageNum , pageSize);
        Page<SmsHomeNewProduct> page1 = homeNewProductService.page(page , lambdaQueryWrapper);

        tableDataInfo.setList(page1.getRecords());

        tableDataInfo.setPageNum(pageNum);

        tableDataInfo.setPageSize(pageSize);

        tableDataInfo.setTotal(page1.getTotal());
        tableDataInfo.setTotalPage(page1.getPages());

        return AjaxResult.success(tableDataInfo);
    }


    /*
    * 4.批量修改首页新品状态
    * */
    @PostMapping("update/recommendStatus")
    public AjaxResult BatchAlterStatus(@RequestParam List<Integer> ids ,Integer recommendStatus)
    {


        ArrayList<SmsHomeNewProduct> Product = new ArrayList<>();


        for (Integer id : ids) {
            SmsHomeNewProduct smsHomeNewProduct = new SmsHomeNewProduct();
            smsHomeNewProduct.setId(id);
            smsHomeNewProduct.setRecommendStatus(recommendStatus);
            Product.add(smsHomeNewProduct);
        }

        boolean b = homeNewProductService.updateBatchById(Product);

        return AjaxResult.success(b);
    }


    /*
    * 5.修改新品排序
    * */
    @PostMapping("update/sort/{id}")
    public AjaxResult AlterSort(@PathVariable Integer id , @RequestBody Integer sort)
    {

        SmsHomeNewProduct Product = new SmsHomeNewProduct();
        Product.setProductId(id);
        Product.setSort(sort);
        Boolean b =  homeNewProductService.updateById(Product);

        return AjaxResult.success(b);
    }
}
